/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#pragma once

#include "datalistener.h"
#include <vector>
#include <QMutex>
#include "iodll.h"

namespace greenhills {
namespace io {

    class CConnection;

    /**
     * @brief This class represents abstract server-side connection.
     *
     * @author Alexander Nikolskiy (alexander.nikolskiy@tieto.com)
     */
    class GH_IO_CLASS CServerConnection : public IDataListener
    {
    public:
        /**
         * @brief Initializes the instance.
         */
        CServerConnection();

        /**
         * @brief Destroys the instance.
         */
        virtual ~CServerConnection();

    public:
        /**
         * @brief Adds a message to the sending queue.
         *
         * @param pData Pointer to data to be sent.
         * @param pLength Length of data to be sent.
         */
        virtual void sendMessage(const char *pData, unsigned int pLength);

        /**
         * @brief Processes sending and receiving operations.
         */
        virtual void process();

        /**
         * @brief Closes this connection.
         */
        virtual void close();

        /**
         * @brief Informs all attached data listeners about new received
         * message.
         *
         * This method just calls emitDataReceivedEvent() method. It's because
         * instance of this class is registered as data listener with all its
         * subconnections.
         *
         * @param pData Pointer to the received data.
         * @param pLength Length of the received data.
         * @param pConnection Pointer to the connection object which sent the
         * data received event.
         */
        virtual void dataReceived(const char *pData,
                                  unsigned int pLength,
                                  CConnection *pConnection);

        /**
         * @brief Adds to the list a listener that will be informed of newly
         * received data.
         *
         * @param pListener Pointer to the listener to add to the list.
         */
        void addDataListener(IDataListener *pListener);

        /**
         * @brief Removes specified listener from the list of attached
         * listeners.
         *
         * @param pListener Pointer to the listener to be removed from the list.
         */
        void removeDataListener(IDataListener *pListener);

        /**
         * @brief Removes all attached listeners from the list.
         */
        void removeAllDataListeners();

    protected:
        /**
         * @brief Informs all attached listeners of the event that new
         * message was received.
         *
         * @param pData Pointer to the data of the received message.
         * @param pLength Length of the data of the received message.
         * @param pConnection Pointer to the connection object which sent the
         * data received event.
         */
        void emitDataReceivedEvent(const char *pData,
                                   unsigned int pLength,
                                   CConnection *pConnection);

    protected:
        /**
         * @brief List of subconnections.
         */
        std::vector<io::CConnection*> mConnectionList;

        /**
         * @brief This mutex that is used when working with listeners list.
         */
        QMutex mMutex;

    private:
        /**
         * @brief List of data listeners.
         */
        std::vector<io::IDataListener*> mDataListenerList;
    };

} //namespace io
} //namespace greenhills
